
# Change error action preference to stop so the script doesn't keep going if we encounter an error.
$ErrorActionPreference = "Stop"

try {
    # Get path to modules.
    $utilModule = (Get-ChildItem -Path $PSScriptRoot -Filter "LSInsightUtilities.psd1" -Recurse).DirectoryName
    $insightModule = (Get-ChildItem -Path $PSScriptRoot -Filter "LSInsight.psd1" -Recurse).DirectoryName



    # Remove LS Insight module if installed
    if ((Get-Module LSInsight)) {
          Remove-Module LSInsight
    }
   
    # Remove LS Insight Utilities module if installed
    if ((Get-Module LSInsightUtilities)) {
        Remove-Module LSInsightUtilities
    }
    
    # Import both modules
    Import-Module $utilModule
    Import-Module $insightModule

    # Check for the Invoke-Sqlcmd function and if it does not exist install and import the sql server module
    try
    {
        Get-Command -Name Invoke-sqlcmd | Out-Null
    }
    catch{
        Write-Host #Newline
        Write-Host 'Installing SqlServer module. This might take a few minutes...'
        Write-Host #Newline
        Install-Module -Name SqlServer
        Import-Module -Name SqlServer
    }
     #
     Write-Host 'Welcome to the LS Insight update script!'
     Write-Host 
     # Collect necessary parameters.
     $title    = 'Get project Parameters'
     $question = 'If you have the full path to the latest LS Insight project parameters file (Parameters.json), you can reuse the parameters for the update. The file should be located in the project folder that was created when LS Insight was deployed (YYYYMMDDHHMM-MyProject). If you do not have access to the file you can enter the parameters again. Do you have the file path?'
     $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Yes I have the parameter file path."
     $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No - I will enter the parameters manually."
     $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
     $decision = $Host.UI.PromptForChoice($title, $question, $choices, 0)

     # Get parameters object from previous installation.
     if ($decision -eq 0) {
         $ProjectParameterFile = Get-LSUpdateProjectParameters
     $params = Read-LSParameters -FilePath $ProjectParameterFile 
     $params.LSInsight.ProjectFolder = $null
     $params.LSInsight.ParametersOutFilePath = $null
     $params.LSInsight.ArmTemplateFile = $null
     $params.LSInsight.ArmTemplateParameterFile = $null
     Write-Host #Newline
     Write-Host 'Existing Parameters have been read from file.'
     Write-Host #Newline
     Write-Host 'When prompted please do the following:'
     Write-Host '1. Log into Azure'
     Write-Host '2. Verify the user name for the LS Central SQL user'
     Write-Host '3. Enter the password for the LS Central SQL user'
     Write-Host '4. Verify the Companies'
     Write-Host '5. Verify all LS Central Parameters'
     Write-Host '6. Verify the user name for the LS Insight db admin user'
     Write-Host '7. Enter the password for the LS Insight db admin user that you selected when LS Insight was originally deployed.'
     Write-Host '8. Verify the LS Insight parameters.'
     Write-Host #Newline
     $params = Get-LSParameters -Parameters $params -ConfirmAll
     } 
     else {
            # Create parameters object.
            $params = New-LSParameters 
            $params.LSInsight.Database.UseExisting = 'True'
            Get-LSParameters -Parameters $params -Overwrite | Out-Null
     }

    # Update LS Insight.
    Update-LSInsight -Parameters $params 
}
catch {
    # Display error message.
    Write-Warning "Something went wrong :("
    Write-Host $_.Exception.Message -ForegroundColor 'Red'

    # Write error log.
}